﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace MusicDatabase
{
    /*
     * Auteur:  Joris Molnár en Niels van Leusden
     * Datum:   31-1-2013
     * Omschrijving: Deze klasse laat de gebruiker inloggen.
     *               Laat het hoofdschermverschijnen wanneer dit succesvol is.
     *               Keert terug op het inlogscherm wanneer dit niet zo is.
     *               Sluit het hele programma wanneer het inlogscherm gesloten wordt.
     *               En opent het registerscherm voor de gebruiker die nog geen account heeft.
     * 
     */
    public partial class Login : Form
    {
        
        private bool goodLogin = false;
        private string nick;

        public string Nick
        {
            get { return nick; }
        }
        
        public Login()
        {
            InitializeComponent();
            if(txtPass.Text.Contains(','))
            {
                txtPass.Text = "";
                MessageBox.Show("Password can't contain ','");
                return;

            }
            else if (txtUser.Text.Contains(','))
            {
                txtUser.Text = "";
                MessageBox.Show("Username can't contain ','");
                return;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            TryLogin();
        }

        /// <summary>
        /// Try to login and store the username that returns
        /// </summary>
        private void TryLogin()
        {
            User user = new User();
            goodLogin = user.Login(txtUser.Text, txtPass.Text, ref nick);
            if (goodLogin)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("Invalid username or password");
                txtPass.Text = "";
                txtUser.Text = "";
            }
        }

        /// <summary>
        /// When the form closes without a succesfull login, the program exits
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!goodLogin) Application.Exit();
        }

        /// <summary>
        /// Open the register dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void llbRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Register register = new Register();
            register.ShowDialog();
        }

        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) TryLogin();
        }
    }
}
